#include "../../include/MyRedisPlus.h"
#include "../../include/Configuration.h"

MyRedisPlus* MyRedisPlus::_pInstance = nullptr;
pthread_once_t MyRedisPlus::_once = PTHREAD_ONCE_INIT;

MyRedisPlus * MyRedisPlus::getInstance()
{
    pthread_once(&_once,init);
    return _pInstance;
}
void MyRedisPlus::init()
{
    _pInstance = new MyRedisPlus();
    atexit(destory);
}
void MyRedisPlus::destory()
{
    if(_pInstance)
    {
        delete _pInstance;
        _pInstance = nullptr;
    }
}

/* bool MyRedisPlus::connect(const string &host,const int port) */
/* { */
/*     _pConnect = redisConnect(host.c_str(), port); */
/*     if(_pConnect != nullptr && _pConnect->err) */
/*     { */
/*         cout << "connect error : " << _pConnect->errstr << endl; */
/*         return false; */
/*     } */
/*     return true; */
/* } */
string MyRedisPlus::get(string key)
{

    auto value = _redis.get(key);
    if(value)
    {
        return value.value();
    }
    else
    {
        return string();
    }
}
void MyRedisPlus::set(string key,string value)
{
    _redis.set(key,value);
}

MyRedisPlus::MyRedisPlus()
        //:_redis("tcp://localhost:6379")
        : _redis(Configuration::getInstance()->getConfigValue("REDIS_PROTOCAL") + "://"
                 + Configuration::getInstance()->getConfigValue("REDIS_HOST") + ":"
                 + Configuration::getInstance()->getConfigValue("REDIS_PORT"))
{
}
